package com.sniff.atp.constant.config;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

@Component
public class DBConnection {
    @Resource
    private DBConfig dbConfig;

    private static final Map<String, Connection> CONNECTION = new HashMap<>();

    public void initDBConnection() {
        dbConfig.getDatasource().forEach((k, v) -> {
            try {
                Class.forName(v.getDriverClassName());
                Connection connection = DriverManager.getConnection(v.getUrl(), v.getUsername(), v.getPassword());
                DBConnection.CONNECTION.put(k, connection);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    public static Connection getConnection(String dbName) {
        return CONNECTION.get(dbName);
    }
}
